package nciae.db.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import nciae.db.common.CommonResult;
import nciae.db.pojo.dto.ApiGetHomeWorkDTO;
import nciae.db.pojo.dto.ApiSubmitHomeWorkDTO;
import nciae.db.pojo.dto.ApiSubmitHomeWorkDTOPlus;
import nciae.db.pojo.dto.apiGetHomeWorkDetailDTO;
import nciae.db.pojo.entity.Classs;
import nciae.db.pojo.entity.Homework;
import nciae.db.pojo.entity.Project;
import nciae.db.pojo.entity.User;
import nciae.db.pojo.vo.ApiGetHomeWorkVO;
import nciae.db.pojo.vo.HomeworkVo;
import nciae.db.pojo.vo.ApiGetHomeWorkDetailVO;
import nciae.db.service.HomeworkService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@CrossOrigin
public class HomeworkController {
    @Autowired
    private HomeworkService homeworkService;
    @ResponseBody
    @RequestMapping("/homework/list")
    public CommonResult<List<HomeworkVo>> list(int page, int limit, String project , String classs, String homeworkType, HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer chargerId=user.getId();
        //System.out.println("chargerId:"+chargerId+"projectName: " + project + ", className: " + classs + ", homeworkType: " + homeworkType + ", page: " + page + ", limit: " + limit);
       return homeworkService.search(page,limit,project,classs,homeworkType,chargerId);
    }
    @GetMapping("/homework/form")
    public ModelAndView form(Integer id){
        HomeworkVo homework;
        if(id == 0){
            homework = new HomeworkVo();
            homework.setId(0);
            // Initialize other fields to prevent nulls
            homework.setProjectId(0);
            homework.setTitle("");
//            homework.setUserId(0);
            homework.setBeginDate(new java.sql.Timestamp(System.currentTimeMillis()));
            homework.setEndDate(new java.sql.Timestamp(System.currentTimeMillis()));
            homework.setHomeworkType("");
//            homework.setStudentNum(0);
            homework.setOverNum(0);
            homework.setSubmitNum(0);
            homework.setClassId(0);
            Classs classs = new Classs();
            classs.setClassName("");
            homework.setClasss(classs);
            Project project = new Project();
            project.setProjectName("");
            homework.setProject(project);
        } else {
            homework= homeworkService.getHomework(id);
            if(homework == null){
                return new ModelAndView("error/404");
            }
        }
        ModelAndView view = new ModelAndView("homework/form");
        view.addObject("model", homework);
        return view;
    }
    @PostMapping("/homework/save")
    public CommonResult<Homework> save(@RequestBody HomeworkVo homeworkVo){
        return homeworkService.homeworkSave(homeworkVo);
    }




    @PostMapping("/api/apiGetHomeWork")
    @ResponseBody
    public CommonResult<List<ApiGetHomeWorkVO>> apiGetHomeWork(@RequestBody ApiGetHomeWorkDTO apiGetHomeWorkDTO){
        Long projectId = apiGetHomeWorkDTO.getProjectId();
        Long classId = apiGetHomeWorkDTO.getClassId();
        Long studentId = apiGetHomeWorkDTO.getStudentId();
        CommonResult<List<ApiGetHomeWorkVO>> result;
        if( projectId == null || classId == null || studentId == null){
            result = new CommonResult<>();
            result.setCode(-1);
            result.setMsg("参数错误");
            return result;
        }
        result = homeworkService.apiGetHomeWork(projectId,classId,studentId);
        return result;
    }


    @PostMapping("/api/apiSubmitHomeWork")
    @ResponseBody
    public CommonResult<Boolean> apiSubmitHomeWork(@RequestBody ApiSubmitHomeWorkDTO apiSubmitHomeWorkDTO){
        CommonResult<Boolean> result = new CommonResult<>();
        Integer homeworkId = apiSubmitHomeWorkDTO.getHomeworkId();
        Integer studentId = apiSubmitHomeWorkDTO.getStudentId();
        String content = apiSubmitHomeWorkDTO.getContent();
        if(homeworkId == null || studentId == null || content == null || StringUtils.isBlank(content)){
            result.setCode(-1);
            result.setMsg("参数错误");
            return result;
        }
        result = homeworkService.apiSubmitHomeWorkNoFiles(homeworkId,studentId,content);
        return result;
    }



    //回显作业信息
    @PostMapping("/api/apiGetHomeWorkDetail")
    @ResponseBody
    public CommonResult<ApiGetHomeWorkDetailVO> apiGetHomeWorkDetail(@RequestBody apiGetHomeWorkDetailDTO apiGetHomeWorkDetailDTO){
        CommonResult<ApiGetHomeWorkDetailVO> result = new CommonResult<>();
        result = homeworkService.apiGetHomeWorkDetail(apiGetHomeWorkDetailDTO);
        return result;
    }


    //删除批量作业
    @PostMapping("/homework/{ids}")
    public CommonResult<Boolean> deleteHomeworks(@RequestParam List<Integer> ids) {
        System.out.println("ok------------------------------------------------------------------------------------------------------------");
        System.out.println(ids);
        return homeworkService.deleteHomeworkById(ids);
    }

}
